Skip to Content
πŸŽ‰ Welcome to JS World!!! πŸŽ‰

20μž₯ strict mode

strict modeλž€?

Strict Mode(엄격 λͺ¨λ“œ)λŠ” JavaScriptμ—μ„œ 더 μ—„κ²©ν•œ 문법과 μ‹€ν–‰ κ·œμΉ™μ„ μ μš©ν•˜λŠ” κΈ°λŠ₯이닀. μ‹€μˆ˜λ‚˜ 버그λ₯Ό λ°©μ§€ν•˜κ³ , μ•ˆμ „ν•˜κ³  효율적인 μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ„λ‘ 도와쀀닀.

  • μ‹€μˆ˜ 방지: λ³€μˆ˜ μ„ μ–Έ 없이 μ‚¬μš©ν•˜κ±°λ‚˜ 잘λͺ»λœ μ½”λ“œλ₯Ό μž‘μ„±ν•˜λ©΄ μ—λŸ¬λ₯Ό λ°œμƒμ‹œν‚΄.
  • λ³΄μ•ˆ κ°•ν™”: this 값이 μ˜λ„μΉ˜ μ•Šκ²Œ μ „μ—­ 객체λ₯Ό μ°Έμ‘°ν•˜μ§€ μ•Šλ„λ‘ 방지.
  • with λ¬Έ μ‚¬μš© κΈˆμ§€: 가독성과 디버깅을 μ–΄λ ΅κ²Œ λ§Œλ“œλŠ” with 문을 κΈˆμ§€.

μ‚¬μš© μ˜ˆμ‹œ)

μ „μ—­μ˜ 선두 λ˜λŠ” ν•¨μˆ˜ λͺΈμ²΄μ˜ 선두에 'usestrict'; λ₯Ό μΆ”κ°€

"use strict"; function foo() { x = 10; // ReferenceError: x is not defined } foo();
function foo() { "use strict"; x = 10; // ReferenceError: x is not defined } foo();
  • ES6 λͺ¨λ“ˆμ€ Strict Mode 기본으둜 적용 λ˜μ–΄μžˆμŒ

  • μ½”λ“œμ—λ””ν„°μ— ESLintλ₯Ό μ„€μΉ˜ν•΄λ„ 같은 효과λ₯Ό λ³Ό 수 있음

전역에 Strict Mode μ μš©μ€ ν”Όν•˜μž

1. μ™ΈλΆ€ λΌμ΄λΈŒλŸ¬λ¦¬μ™€μ˜ 좩돌 κ°€λŠ₯μ„±

전역에 Strict Modeλ₯Ό μ μš©ν•˜λ©΄, λΌμ΄λΈŒλŸ¬λ¦¬λ‚˜ ν”ŒλŸ¬κ·ΈμΈ μ½”λ“œμ—λ„ κ°•μ œλ‘œ Strict Modeκ°€ μ μš©λœλ‹€. 일뢀 μ™ΈλΆ€ λΌμ΄λΈŒλŸ¬λ¦¬λ‚˜ 였래된 μ½”λ“œλŠ” Strict Modeλ₯Ό κ³ λ €ν•˜μ§€ μ•Šκ³  μž‘μ„±λ˜μ—ˆκΈ° λ•Œλ¬Έμ—, κ°‘μž‘μŠ€λŸ½κ²Œ λ™μž‘ν•˜μ§€ μ•Šκ±°λ‚˜ 였λ₯˜λ₯Ό λ°œμƒμ‹œν‚¬ 수 μžˆλ‹€.

2. μ „μ—­ Strict Mode 적용이 μ½”λ“œ 뢄리와 독립성을 ν•΄μΉ¨

Strict Modeλ₯Ό 전역에 μ μš©ν•˜λ©΄, ν”„λ‘œμ νŠΈμ˜ λͺ¨λ“  μ½”λ“œκ°€ 같은 μ»¨ν…μŠ€νŠΈμ—μ„œ μ‹€ν–‰λœλ‹€. μ΄λŠ” λͺ¨λ“ˆν™”와 μ½”λ“œμ˜ 독립성을 ν•΄μΉ˜κ³ , νŠΉμ • νŒŒμΌμ—μ„œ μ˜λ„ν•˜μ§€ μ•Šμ€ μ—λŸ¬κ°€ λ°œμƒν•  κ°€λŠ₯성을 높인닀.

ν•¨μˆ˜ λ‹¨μœ„λ‘œ strict modeλ₯Ό μ μš©ν•˜λŠ” 것도 ν”Όν•˜μž (?)

μœ„μ—μ„œ μ‹€μ»· 전역에 쓰지말라 해놓고 ν•¨μˆ˜λ‹¨μœ„λ‘œλ„ μ“°μ§€λ§λž˜μ„œ 읭?ν–ˆλŠ”λ° μΌκ΄€λ˜κ²Œ μ μš©ν•˜κ³  싢은 κ²½μš°μ—” μ¦‰μ‹œμ‹€ν–‰ν•¨μˆ˜λ₯Ό μ΄μš©ν•  수 μžˆλ‹€κ³  ν•©λ‹ˆλ‹€

function strictFunction() { "use strict"; let x = 10; // Strict Mode 적용 } function nonStrictFunction() { y = 20; // Strict Mode 미적용 }

파일 μ „μ²΄μ—μ„œ Strict Modeλ₯Ό μΌκ΄€λ˜κ²Œ μ μš©ν•˜μ§€ λͺ»ν•΄, μ½”λ“œ 가독성과 μœ μ§€λ³΄μˆ˜μ„±μ΄ λ–¨μ–΄μ§ˆ 수 있음.

κ·Έλ•Œμ—λŠ”! μ¦‰μ‹œ μ‹€ν–‰ ν•¨μˆ˜λ‘œ 전체 μŠ€ν¬λ¦½νŠΈμ— 적용 κ°€λŠ₯

(function () { "use strict"; function strictFunction() { let x = 10; // Strict Mode 적용 } function nonStrictFunction() { y = 20; // ReferenceError: y is not defined } })();

파일 λ‚΄ λͺ¨λ“  μ½”λ“œκ°€ λ™μΌν•œ κ·œμΉ™(Strict Mode) μ•„λž˜μ—μ„œ μ‹€ν–‰λ˜λ―€λ‘œ, 일관성과 μ•ˆμ •μ„±μ΄ 보μž₯됨.

strict modeκ°€ λ°œμƒμ‹œν‚€λŠ” μ—λŸ¬

  1. 암묡적 μ „μ—­

    (function () { "use strict"; x = 10; console.log(x); // ReferenceError: x is not defined })();
  2. delete μ—°μ‚°μžλ‘œ λ³€μˆ˜,ν•¨μˆ˜, λ§€κ°œλ³€μˆ˜λ₯Ό μ‚­μ œ

    (function () { "use strict"; var x = 1; delete x; // SyntaxError: Delete of an unqualified identifier in strict mode. function foo(a) { delete a; // SyntaxError: Delete of an unqualified identifier in strict mode. } delete foo; // SyntaxError: Delete of an unqualified identifier in strict mode. })();
  3. λ§€κ°œλ³€μˆ˜ μ΄λ¦„μ˜ 쀑볡

    (function () { "use strict"; //SyntaxError: Duplicate parameter name not allowed in this context function foo(x, x) { return x + x; } console.log(foo(1, 2)); })();
  4. with 문의 μ‚¬μš©

    with 문은 μ „λ‹¬λœ 객체λ₯Ό μŠ€μ½”ν”„ 체인에 μΆ”κ°€ν•œλ‹€. with 문은 λ™μΌν•œ 객체의 ν”„λ‘œνΌν‹°λ₯Ό λ°˜λ³΅ν•΄μ„œ μ‚¬μš©ν•  λ•Œ 객체 이름을 μƒλž΅ν•  수 μžˆμ–΄μ„œ μ½”λ“œκ°€ κ°„λ‹¨ν•΄μ§€λŠ” νš¨κ³Όκ°€ μžˆμ§€λ§Œ μ„±λŠ₯κ³Ό 가독성이 λ‚˜λΉ μ§€λŠ” λ¬Έμ œκ°€ μžˆλ‹€.

    (function (){ 'use strict'; // SyntaxError: Strict mode code may not include a with statement with { x: 1 }) { console.log(x); } }());

strict mode μ μš©μ— μ˜ν•œ λ³€ν™”

  1. μΌλ°˜ν•¨μˆ˜μ˜ this strict modeμ—μ„œ ν•¨μˆ˜λ₯Ό 일반 ν•¨μˆ˜λ‘œμ„œ ν˜ΈμΆœν•˜λ©΄ this에 undefinedκ°€ 바인딩 λœλ‹€. μƒμ„±μžν•¨μˆ˜κ°€ μ•„λ‹Œ 일반 ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œλŠ” thisλ₯Ό μ‚¬μš©ν•  ν•„μš”κ°€ μ—†κΈ° λ•Œλ¬Έ.

  2. arguments 객체

    strict modeμ—μ„œλŠ” λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬λœ 인수λ₯Ό μž¬ν• λ‹Ήν•˜μ—¬ 변경해도 arguments 객체에 λ°˜μ˜λ˜μ§€ μ•ŠλŠ”λ‹€.

    (function (a) { "use strict"; // λ§€κ°œλ³€μˆ˜μ— μ „λ‹¬λœ 인수λ₯Ό μž¬ν• λ‹Ήν•˜μ—¬ λ³€κ²½ a = 2; // λ³€κ²½λœ μΈμˆ˜κ°€ arguments 객체에 λ°˜μ˜λ˜μ§€ μ•ŠλŠ”λ‹€. console.log(arguments); // { 0: 1, length: 1 } })(1);
Last updated on